package com.atguigu.auth.activiti;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class ProcessTestJiaban {

    //注入RepositoryService,部署项目
    @Autowired
    private RepositoryService repositoryService;


    //注入RuntimeService 用于开始一个新的流程实例
    @Autowired
    private RuntimeService runtimeService;

    //TaskService 查询任务
    @Autowired
    private TaskService taskService;

    //查询已经处理的任务
    @Autowired
    private HistoryService historyService;

    /**
     * 部署流程定义
     */
    @Test
    public void deployprocess01(){
        //流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban.bpmn20.xml")
                .addClasspathResource("process/加班.png")
                .name("加班申请流程")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    /**
     *启动实例流程
     */
    @Test
    public void startProcessInstance(){
        Map<String,Object> map = new HashMap<>();
        //设置任务人
        map.put("assignee1","lucy");
        map.put("assignee2","mary");
        ProcessInstance jiaban = runtimeService.startProcessInstanceById("process:1:36b9d843-b387-11ee-9046-0a0027000007", map);
        System.out.println(jiaban.getId());
        System.out.println(jiaban.getName());
    }

    /**
     * 查询个人的代办任务
     * 通过方法查询请假流程，
     */
    @Test
    public void findTaskList(){
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("jack")
                .list();
        for (Task task : list) {
            System.out.println("流程实例："+task.getProcessInstanceId());
            System.out.println("任务id："+task.getId());
            System.out.println("任务负责人："+task.getAssignee());
            System.out.println("任务名称："+task.getName());
        }
    }



    /**
     * 部署流程定义2
     */
    @Test
    public void deployprocess02(){
        //流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/qingjiabyfunproces.bpmn20.xml")
                .addClasspathResource("process/通过方法完成请假流程.png")
                .name("加班申请流程(通过方法的形式实现)")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void startUpProcess02(){
        //创建流程实例，我们需要知道流程定义的key
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("qingjiabyfunproces");
        //输出实例的相关信息
        System.out.println("流程定义id：" + processInstance.getProcessDefinitionId());
        System.out.println("流程实例id：" + processInstance.getId());

    }

    /**
     * 部署流程实例
     */
    @Test
    public void startProcess03(){
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/qingjiabylisteners.bpmn20.xml")
                .addClasspathResource("process/监听器的形式实现.png")
                .deploy();
        System.out.println(deploy.getName());
        System.out.println(deploy.getKey());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void startProcess(){
        ProcessInstance qingjiabylisteners = runtimeService.startProcessInstanceByKey("qingjiabylisteners");
        System.out.println(qingjiabylisteners.getName());
        System.out.println(qingjiabylisteners.getBusinessKey());
    }





}
